﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Globalization;
using System.Collections.ObjectModel;

namespace University.Workshop.Business
{
    public enum RegisterError { FName, LName, FormatEmail, BusinessEmail, FormatPass, REPass, City, Mobile, Phone, BirthNull, BlankNull }
    public static class UserRegistrationManager
    { 
        public static Collection<RegisterError> RegisterUser( String firstName, String lastName, char gender,
                                         DateTime birthday, String email, String password, String rePassword, String department,
                                         String city, String mobile, String phone)
        {
            Collection<RegisterError> errors = new Collection<RegisterError>();

            if (DateTime.Compare(birthday,  DateTime.MinValue)==0)
            {
                errors.Add(RegisterError.BirthNull);
            }

            string AlphabeticPattern = @"^[a-zA-Z ñÑ]+$";
            if (string.IsNullOrWhiteSpace(firstName))
            {
                errors.Add(RegisterError.BlankNull);
            }
            else if (!Regex.IsMatch(firstName, AlphabeticPattern))
            {
                errors.Add(RegisterError.FName);
            }

            if (string.IsNullOrWhiteSpace(lastName))
            {
                errors.Add(RegisterError.BlankNull);
            }
            else if (!Regex.IsMatch(lastName, AlphabeticPattern))
            {
                errors.Add(RegisterError.LName);
            }

            if (string.IsNullOrWhiteSpace(city))
            {
                errors.Add(RegisterError.BlankNull);
            }
            else if (!Regex.IsMatch(city, AlphabeticPattern))
            {
                errors.Add(RegisterError.City);
            }

            string NumericPattern = @"^[0-9]+$";
            if (string.IsNullOrWhiteSpace(mobile))
            {
                errors.Add(RegisterError.BlankNull);
            }
            else if (!Regex.IsMatch(mobile, NumericPattern))
            {
                errors.Add(RegisterError.Mobile);
            }

            if (string.IsNullOrWhiteSpace(phone))
            {
                errors.Add(RegisterError.BlankNull);
            }
            else if (!Regex.IsMatch(phone, NumericPattern))
            {
                errors.Add(RegisterError.Phone);
            }

            string passwordPattern = "\\w{6,20}";
            string passwordPattern1 = "\\d+";
            if (string.IsNullOrWhiteSpace(password))
            {
                errors.Add(RegisterError.BlankNull);
            }
            else if (!(Regex.IsMatch(password, passwordPattern) &&
                Regex.IsMatch(password, passwordPattern1)))
                errors.Add(RegisterError.FormatPass);

            if (password != rePassword)
            {
                errors.Add(RegisterError.REPass);
            }
            string emailPattern = @"^(?("")(""[^""]+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
                             @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$";
            if (string.IsNullOrWhiteSpace(email))
            {
                errors.Add(RegisterError.BlankNull);
            }
            else if (!Regex.IsMatch(email, emailPattern))
                errors.Add(RegisterError.FormatEmail);
            User existingUser = UserDataAccess.GetUserByEmail(email);
            if (existingUser != null)
            {
                errors.Add(RegisterError.BusinessEmail);
            }

            if (errors.Count == 0) // if weren't errors found
            {
                User newUser = new User
                {
                    PersonalInformation = new PersonalInfo { FirstName = firstName, LastName = lastName, Gender = gender, Birthday = birthday },
                    ContactInformation = new ContactInfo { Email = email, Department = department, City = city, MobilePhone = int.Parse(mobile, CultureInfo.CurrentCulture), Phone = int.Parse(phone, CultureInfo.CurrentCulture) },
                    Password = password,
                    Status = AccountState.Active
                };
                UserDataAccess.InsertUser(newUser);
            }
            return errors;
        }
    }
}
